<?php
namespace App\Api\Controllers;
use Illuminate\Http\Request;
use App\Api\Services\ImageLabelService;
use App\Http\Controllers\Api\BaseController;
use Illuminate\Support\Facades\Validator;

class ImageLabelController extends BaseController
{
    
    /**
     * 添加图片画册标签
     * @param is_top     置顶
     * @param label_name 标签名称
     * @param sort_number 排序数字
     */
    public function addImageLabel(Request $request){

        $validator = Validator::make(
            $data  = $request->all(), [
                'pa_id' =>'required'
            ],[
                'pa_id.required' => '画册的id,不能为空!'
            ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
             
            return response()->json(['code' => 1,'msg' => $info,'data' => '']);
        }
     if(!empty($data['is_top']) && $data['is_top']==1){
            
            $condition['is_top']       = 1;
            $condition['pa_id']        = $data['pa_id'];
            $condition['user_id']      = $this->getUserId();
            $condition['main_id']      = $this->getMainUserId();
    
    
            $info = ImageLabelService::getImageLabelInfo($condition);
    
            if(!empty($info)){
    
                ImageLabelService::updImageLabel($condition, ['is_top'=>0]);
            }
        }
        $data['create_time']  = time();       
        $data['user_id']      = $this->getUserId();
        $data['main_id']      = $this->getMainUserId();        
        $data['is_top']       = empty($data['is_top'])?0:$data['is_top'];        
        $data['sort_number']  = empty($data['sort_number'])?'':$data['sort_number'];
        $data['label_name']   = empty($data['label_name'])?'':$data['label_name'];

        $id = ImageLabelService::addImageLabel($data);
    
        if(!empty($id)){
    
            return response()->json(['code'=>0,'msg'=>'success','data'=>['label_id'=>$id]]);
             
        }else{
            
            return response()->json(['code'=>1,'msg'=>'保存失败','data'=>'']);
        }
    
    }
    
    /**
     * 查询图片画册标签列表
     * 
     * @param curr_page   当前页
     * @param page_size   显示个数
     * @param pa_id       文字画册id
     * @param sort_number 排序数字
     */
    public function getImageLabelList(Request $request){
    
        $currentPage = !empty($request->input('curr_page'))?$request->input('curr_page'):1;
        $pageSize    = !empty($request->input('page_size'))?$request->input('page_size'):20;
    
        $condition['user_id'] = $this->getUserId();       
        $condition['pa_id']   = $request->input('pa_id');
        $sort_number          = !empty($request->input('sort_number'))?$request->input('sort_number'):'';
        
        $list  = ImageLabelService::getImageLabelList($condition,$sort_number,$currentPage,$pageSize);
        /*  echo "<pre>";
         var_dump($list);die; */
        if(!empty($list['data'])){

           foreach ($list['data'] as $ik=>$iv){
               
               if(!empty($iv['Album_imgs'])){
                   
                   $list['data'][$ik]['Album_imgs'] =explode(',',$iv['Album_imgs']); 
               }
                   $list['data'][$ik]['create_time']=date('Y-m-d H:i:s',$iv['create_time']);
                   $list['data'][$ik]['update_time']=date('Y-m-d H:i:s',$iv['update_time']);
           }           
        }
    
        return response()->json(['code'=>0,'msg'=>'success','data'=>$list]);
    }
   
    /**
     * 修改内容标签
     * 
     * @param label_id    内容标签id
     * @param label_name  标签名称
     * @param is_top      置顶
     * @param sort_number 排序数字
     */
    public function updImageLabel(Request $request){
    
        if(empty($ctId = $request->input('label_id'))){
             
            return response()->json(['code'=>1,'msg'=>'内容标签id,不能为空','data'=>'']);
        }

        $condition['id'] = $ctId;
        $condition['user_id'] = $this->getUserId();
     
        $updData = $request->all();
        $updData['update_time']  = time();
        
        if(isset($updData['Album_imgs'])){
            
            unset($updData['Album_imgs']);
        }
        
         $updCondition['is_top']  = 1;
         $updCondition['pa_id']   = $request->input('pa_id');
         $updCondition['user_id'] = $this->getUserId();
         
        $info = ImageLabelService::getImageLabelInfo($updCondition);
        
        if(!empty($info)){
            
            if(isset($updData['is_top'])){
                
                if($updData['is_top'] == 1){
                    
                    ImageLabelService::updImageLabel($updCondition,['is_top'=> 0]); 
                }
            }
        }

        unset($updData['label_id']);unset($condition['is_top']);
        
        $bool = ImageLabelService::updImageLabel($condition,$updData);  
        
        if($bool){
             
            return response()->json(['code'=>0,'msg'=>'修改成功','data'=>'']);
        }
         
        return response()->json(['code'=>1,'msg'=>'修改失败','data'=>'']);
    
    }
    
    /**
     * 删除内容标签
     * @param label_id 内容标签id
     */
    public function delImageLabel(Request $request){
    
        if(empty($ctId = $request->input('label_id'))){
             
            return response()->json(['code'=>1,'msg'=>'内容标签id,不能为空','data'=>'']);
        }
                
        $condition['id'] = $ctId;
        $condition['user_id'] = $this->getUserId();
        
        $info = ImageLabelService::getImageLabelInfo($condition);
        
        if(empty($info)){
            
            return response()->json(['code'=>1,'msg'=>'内容标签，不存在！','data'=>'']);
        }
        
        $bool = ImageLabelService::delImageLabel($condition);
    
        if($bool){
            
            if(!empty($info['Album_imgs'])){
                
            $info['Album_imgs'] = explode(',',$info['Album_imgs']);
            
                    foreach ($info['Album_imgs'] as $k=>$v){
                
                        if(empty($v)){
                             
                            if(is_file(public_path($v))){
                                 
                                unlink(public_path($v));
                            }
                        }
                    }
              
            }
            
            return response()->json(['code'=>0,'msg'=>'删除成功','data'=>'']);
        }
    
        return response()->json(['code'=>1,'msg'=>'删除失败','data'=>'']);
    
    }
       
}
